package org.example.mapper;

import org.apache.ibatis.annotations.*;
import org.example.pojo.Appointment;
import com.baomidou.dynamic.datasource.annotation.DS;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Mapper
@DS("second")

public interface AppointmentMapper {

    // 添加挂号
    @Insert("INSERT INTO appointments (patient_id,doctor_id ,clinic_id, appointment_time, status) " +
            "VALUES (#{patientId},#{doctorId}, #{clinicId}, #{appointmentTime}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "appointmentId")
    void insertAppointment(Appointment appointment);

    // 更新挂号状态
    @Update("UPDATE appointments SET status = #{status} WHERE appointment_id = #{appointmentId}")
    void updateAppointmentStatus(Integer appointmentId, String status);

    // 获取挂号列表
    @Select("SELECT * FROM appointments")
    List<Appointment> getAppointments();

    // 查询未支付且超过10分钟的挂号
    @Select("SELECT * \n" +
            "FROM appointments \n" +
            "WHERE status = '待支付' \n" +
            "  AND created_at < now() - INTERVAL 10 MINUTE;")
    List<Appointment> findUnpaidAppointments(@Param("date") Timestamp date);


    // 查询已过期的挂号
    @Select("SELECT * FROM appointments WHERE appointment_time < curdate() AND status != '已叫号'")
    List<Appointment> findExpiredAppointments();
    @Update("UPDATE appointments SET status = #{status} WHERE appointment_time = #{appointmentTime}")
    void updateAppointmentStatusByTime(Date appointmentTime, String status);
    // 查询超出近七天的数据
    @Select("SELECT *\n" +
            "FROM appointments\n" +
            "WHERE appointment_time < DATE_SUB(NOW(), INTERVAL 7 DAY);")
    List<Appointment> getSevenAppointments();
    // 删除挂号
    @Delete("DELETE FROM appointments WHERE appointment_id = #{appointmentId}")
    void deleteAppointment(Integer appointmentId);
    //根据患者id查询信息
    @Select("select * from appointments where patient_id=#{patientId}")
    List<Appointment> getAppointmentById(Integer patientId);

    @Select("select * from appointments where status='已叫号' and appointment_time = CURDATE() - INTERVAL 1 DAY")
    List<Appointment> findYesterdayAppointmentsByStatus();
    @Select("select * from appointments where (status='已支付' or status='已叫号') and appointment_time=CURDATE()")
    List<Appointment> getAppointmentsByDay();
}
